import json
import random
import unittest

import requests

from lib.HTMLTestRunner_PY3 import HTMLTestRunner


def read_info(filename='data/login_data.json'):
    temp_list = []
    with open(filename, 'r', encoding='utf-8') as f:
        content = json.load(f)
        print(content)
        for temp_dict in content:
            temp_list.append(tuple(temp_dict.values()))
    return temp_list


def get_url(key, filename='./data/urls.json'):
    with open(filename, 'r', encoding='utf-8') as f:
        # 读取json内容
        content = json.load(f)

    return content.get(key)


def deal_report(start_dir, pattern, filename):
    # 1.从工作路径下，当前的script文件夹
    suite = unittest.TestLoader().discover(start_dir, pattern)
    # 2.打开文件
    with open(filename, 'wb') as f:
        # 3.报告对象
        runner = HTMLTestRunner(f, verbosity=2)
        # 4.执行套件
        runner.run(suite)


def get_rand_mobile(start='13'):
    # 以start开头，后面跟一位4~9之间的任意一位数字，后面是8位随机数字
    temp = (start +
            str(random.randrange(4, 10)) +
            ''.join(str(random.choice(range(10))) for _ in range(8)))
    return temp


def get_au_header(json):
    """
    功能：组装登陆成功后的认证请求头字典信息
    :param json: 登陆请求体
    :return: 登陆成功后的认证请求头字典信息
    """
    # 1. 登陆
    login_url = get_url('login_url')
    resp = requests.post(login_url, json=json)

    # 2. 提取响应内容的data字段
    # 3. 组装成认证请求头字段
    au_header = {'Authorization': 'Bearer ' + resp.json().get('data')}
    # print(au_header)

    return au_header


if __name__ == '__main__':
    ret = read_info()
    print(ret)
